**********************************************************
*******  This file simulates the Senate vote
******  File needs to be updated with links to new files
******** Also, should save the data after the simulation, so that we can change the graphs

clear 
set more off 
est clear
****  if $toggle==1 outsheet and estout
global toggle = 1  
global date = "210726"

****  supply toggles:  $supplytoggle==2 (corrects trapezoids) $supplytoggle==1 (shifts supply out) $supplytoggle==0 (no correction) 
global supplytoggle=2

**** quantile toggles: $numquant = number of quantiles of rfs gains - cat gains
global numquant=4

*
*global preamble = "C:\AllStephen\My Dropbox\Jon n Chris"
*global preamble = "/Users/crknittel/Dropbox/Landuse"
global preamble = "/Users/Jon/Dropbox/Landuse"
*
global datadirectory = "$preamble/5 - Probits/Data"
global codedirectory = "$preamble/5 - Probits/Code"
global tabledirectory = "$preamble/Write-up/Tables"
global figuredirectory = "$preamble/Write-up/Figures"

use "$datadirectory/cd_pscs&votes_$date$supplytoggle.dta"
probit  vote_d_cat i.dem_cat i.pscs_cat_quart_2 i.pscs_cat_quart_3 i.pscs_cat_quart_4 i.pscs_rfs_quart_2 i.pscs_rfs_quart_3 i.pscs_rfs_quart_4 i.coal_top10
predict vote_probs 
#delimit; 
twoway  
(histogram vote_probs if dem_cat==0 & vote_d_cat==0, w(0.03) frequency fcolor(red)) 
(histogram vote_probs if dem_cat==0 & vote_d_cat==1, w(0.03) frequency fcolor(pink)) 
(histogram vote_probs if dem_cat==1 & vote_d_cat==0, w(0.03) frequency fcolor(ltblue)) 
(histogram vote_probs if dem_cat==1 & vote_d_cat==1, w(0.03) frequency fcolor(blue)),  
xtitle("Predicted Probability of Voting for Waxman-Markey") ytitle("Number of Representatives")  
legend(order(1 "Republican/Nay" 2 "Republican/Aye" 3 "Democrat/Nay" 4 "Democrat/Aye")) 
; 
#delimit cr 

use "$datadirectory/state_pscs_percap_$date$supplytoggle.dta", clear

xtile pscs_rfs_quart = pscs_rfs_percap, nq(4)
xtile pscs_subs_quart = pscs_subs_percap, nq(4)
xtile pscs_cat_quart = pscs_cat_percap, nq(4)

gen pscs_rfs_quart_2 = pscs_rfs_quart==2
gen pscs_rfs_quart_3 = pscs_rfs_quart==3
gen pscs_rfs_quart_4 = pscs_rfs_quart==4

gen pscs_cat_quart_2 = pscs_cat_quart==2
gen pscs_cat_quart_3 = pscs_cat_quart==3
gen pscs_cat_quart_4 = pscs_cat_quart==4

drop if st_abb==""
merge 1:m st_abb using "$datadirectory/Senate_111.dta"
predict senate_vote
drop _merge
 
#delimit; 
twoway  
(histogram senate_vote if dem_cat==0, w(0.03) frequency fcolor(red)) 
(histogram senate_vote if dem_cat==1, w(0.03) frequency fcolor(blue)),  
xtitle("Probability of Voting for Waxman-Markey") ytitle("Number of Senators") legend(order(1 "Republican" 2 "Democrat")) 
; 

global directory = "/Users/crknittel/Dropbox/LandUse/";
histogram senate_vote, w(0.03) frequency 
xlabel(, grid format(%9.2f)angle(horizontal))
scheme(s1mono)
xtitle("Probability of Voting for Waxman-Markey")
ytitle("Number of Senators")
;
 
if $toggle==1{ ; 
graph export "$figuredirectory/senate_histo_$date.pdf", replace;
}; 

#delimit cr
 

// Monte Carlo

set more off 
global numdraws=1000 
 
// get data with informed probabilities 
gen id=_n 
keep id st_abb senate_vote 
 
 
// preliminary examination 
gen greaterthan50pct= senate_vote>.5 
**estpost summarize senate_vote greaterthan50pct, detail 
**esttab, cells("mean sd min max p50") nomtitle nonumber label varwidth(40) 
 
// SIMULATION OF VOTING  
**set seed 55555 
version 11.2
set seed 10101 
tempfile draws currdraw 
preserve 
forvalues iter = 1(1)$numdraws { 
	qui { 
	/// BERNOULLI DRAW WITH PROBABILITY OF EACH SENATE VOTE... DRAWS FOR TOTAL 
		gen vote=uniform()<=senate_vote  // random draw with probability of voting 
		gen voten=rnormal(senate_vote)>.5  // add mean zero, var 1 shock to probability of voting 
		gen pr_eps=normal(invnormal(senate_vote)+rnormal()) // add add mean zero, var 1 shock to probit's x*beta 
		gen votexb=pr_eps>.5 
		* gen votexb=normal(invnormal(senate_vote)+rnormal())>.5  
		 
		gen iter=`iter' 
		save `currdraw', replace 
		 
		/// BOOTSTRAP OBSERVATIONS 
		bsample 
		renpfix "" "boot_" 
		gen id=_n 
		joinby id using `currdraw' 
	} 
	 
	capture append using `draws' 
	qui save `draws', replace 
	restore, preserve 
} 
restore 
 
use `draws', clear 
save draws, replace 
 
// check within senators is correct 
preserve 
collapse (mean) avg_vote=vote avg_voten=voten avg_votexb=votexb, by(id senate_vote) 
gen diff = avg_vote-senate_vote 
label variable diff "senate_vote-mean(uniform()<=senate_vote)" 
gen diffn = avg_voten-senate_vote 
label variable diffn "senate_vote-mean(rnormal(senate_vote)>.5)" 
gen diffxb = avg_votexb-senate_vote 
label variable diffxb "senate_vote-normal(invnormal(senate_vote)+rnormal())>.5" 
 
estpost summarize diff*, detail 
esttab, cells("mean sd min max") nomtitle nonumber label varwidth(40) 
restore 
 
// examine results 
use draws, clear 
collapse (rawsum) vote* boot_greaterthan50pct (mean) boot_pr_vote=boot_senate_vote, by(iter) 
label variable vote "uniform()<=senate_vote" 
label variable voten "rnormal(senate_vote)>.5" 
label variable votexb "normal(invnormal(senate_vote)+rnormal())>.5" 
label variable boot_greaterthan50pct "bootstrapped .5 counts" 
label variable boot_pr_vote "bootstrapped mean prob" 
save totals, replace 
estpost summarize vote* boot_greaterthan50pct, detail 
esttab, cells("mean sd min max p50") nomtitle nonumber label varwidth(30) 
graph drop _all 
hist vote, freq discrete xlabel(#15) nodraw name(grvote) 
hist voten, freq discrete xlabel(#15) nodraw name(grvoten) 
hist votexb, freq discrete xlabel(#15) nodraw name(grvotexb) 
hist boot_greaterthan50pct, freq discrete xlabel(#15) nodraw name(grboot) 
graph combine grvote grvoten grvotexb grboot, xcommon // cols(1) 

if $toggle==1{
graph export histograms.png, replace 

histogram votexb, xlabel(, grid format(%9.0f)angle(horizontal)) scheme(s1mono) xtitle("Number of Senate Aye Votes") ytitle("Frequency") w(1) frequency
graph export "$figuredirectory/senate_MC_histo.pdf", replace 
}
